﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using PSE.Framework.Configuration;

namespace PSE.Framework.ErrorLogging.Configuration
{
    public class ErrorLoggingConfigurationSection : ConfigurationSection
    {
        public const string DefaultLogFilePath = "Logs";
        public const int DefaultDaysToKeepLogFile = 7;

        public const string LogFileName = "Log_{0}.svclog";

        [ConfigurationProperty("logFilePath", IsRequired = true)]
        public string LogFilePath
        {
            get { return (string)this["logFilePath"]; }
            set { this["logFilePath"] = value; }
        }

        [ConfigurationProperty("daysToKeepLogFile", IsRequired = true)]
        //[IntegerValidator(MinValue = 1, MaxValue = int.MaxValue)]
        public int DaysToKeepLogFile
        {
            get { return (int)this["daysToKeepLogFile"]; }
            set { this["daysToKeepLogFile"] = value; }
        }

        [ConfigurationProperty("keepClientErrors", IsRequired = false, DefaultValue = true)]
        public bool KeepClientError
        {
            get { return (bool)this["keepClientErrors"]; }
            set { this["keepClientErrors"] = value; }
        }

        [ConfigurationProperty("sources", IsRequired = true)]
        [ConfigurationCollection(typeof(LogSourceConfigurationCollection), AddItemName = "source")]
        public LogSourceConfigurationCollection Sources
        {
            get { return (LogSourceConfigurationCollection)this["sources"]; }
        }

        public string GetLogFilePath(SystemComponent system)
        {
            string logFilePath = this.LogFilePath;

            if (Path.IsPathRooted(logFilePath))
                logFilePath = Path.GetFullPath(logFilePath);
            else
                logFilePath = Path.GetFullPath(Path.Combine(ApplicationConfiguration.GetApplicationRootDirectory(system), logFilePath));

            return logFilePath;
        }

        public string GetLogFileName(SystemComponent system)
        {
            return Path.Combine(this.GetLogFilePath(system), LogFileName);
        }
    }
}
